package com.ruoyi.rms.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.rms.domain.OrderMessage;
import com.ruoyi.rms.service.IDishesOrderService;
import com.ruoyi.rms.service.IOrderMessageService;
import com.ruoyi.rms.utils.IdUtils;
import com.ruoyi.rms.utils.RandomForestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.rms.mapper.DishesMessageMapper;
import com.ruoyi.rms.domain.DishesMessage;
import com.ruoyi.rms.service.IDishesMessageService;
import weka.classifiers.trees.RandomForest;
import weka.core.Attribute;
import weka.core.Instance;
import weka.core.Instances;

/**
 * 菜品信息Service业务层处理
 * 
 * @author wyl
 * @date 2024-03-07
 */
@Service
public class DishesMessageServiceImpl implements IDishesMessageService 
{
    @Autowired
    private DishesMessageMapper dishesMessageMapper;

    /**
     * 查询菜品信息
     * 
     * @param dishesId 菜品信息主键
     * @return 菜品信息
     */
    @Override
    public DishesMessage selectDishesMessageByDishesId(Long dishesId)
    {
        return dishesMessageMapper.selectDishesMessageByDishesId(dishesId);
    }

    /**
     * 分组查询菜品和总销量
     * @return
     */
    @Override
    public List<DishesMessage> selectByDishesIdAndAccount() {
        return dishesMessageMapper.selectByDishesIdAndAccount();
    }

    /**
     * 查询菜品信息列表
     * 
     * @param dishesMessage 菜品信息
     * @return 菜品信息
     */
    @Override
    public List<DishesMessage> selectDishesMessageList(DishesMessage dishesMessage)
    {
        return dishesMessageMapper.selectDishesMessageList(dishesMessage);
    }

    /**
     * 查询推荐菜品
     * @return
     */
    @Override
    public List<DishesMessage> selectRecommendDishes() {
        List<DishesMessage> dishesMessageList = dishesMessageMapper.selectDishesByRecommend();
        RandomForestUtils randomForestUtils = new RandomForestUtils();

        ArrayList<Attribute> trainAttributes = randomForestUtils.initAttribute(); // 初始化训练集指标
        Instance instance = randomForestUtils.initInstance(trainAttributes); // 初始化训练集实例

        for (DishesMessage dishesMessage : dishesMessageList) {
            BigDecimal price = dishesMessage.getPrice();
            Integer orderNum = dishesMessage.getOrderNum();
            Integer score = dishesMessage.getValue();

            randomForestUtils.loadTrainData(trainAttributes, instance, price.doubleValue(), orderNum, score); // 将训练集载入模型
        }

        RandomForest randomForest = randomForestUtils.train(RandomForestUtils.data); // 训练，返回结果模型

        ArrayList<Attribute> testAttributes = randomForestUtils.initAttribute(); // 初始化测试集指标

        // 查询各菜品的平均下单量和所在订单的平均得分
        List<DishesMessage> dishesList = dishesMessageMapper.selectDishesWithOrderNumAndScore();
        for (DishesMessage dishesMessage : dishesList) {
            BigDecimal price = dishesMessage.getPrice();
            Integer orderNum = dishesMessage.getOrderNum();
            Integer score = dishesMessage.getValue();

            Instances testData = randomForestUtils.loadTestData(testAttributes, price.doubleValue(), orderNum, score); // 将测试集载入模型
            double result = randomForestUtils.test(randomForest, testData);  // 用训练好的模型进行测试
            dishesMessage.setResult(result); // 获取测试结果
        }

        // 要返回的结果
        List<DishesMessage> list = new ArrayList<>();

        // 按菜品类别分组
        Map<Long, List<DishesMessage>> collect = dishesList.stream().collect(Collectors.groupingBy(DishesMessage::getCategoryId));
        for (Map.Entry<Long, List<DishesMessage>> entry : collect.entrySet()) {
            // 选出每个类中得分最多的菜品
            List<DishesMessage> dishesMessages = entry.getValue();
            DishesMessage dishesMessage = dishesMessages.stream().max(Comparator.comparing(DishesMessage::getResult)).orElse(null);
            if (dishesMessage != null) {
                DishesMessage dishes = dishesMessageMapper.selectDishesMessageByDishesId(dishesMessage.getDishesId());
                list.add(dishes);
            }
        }

        return list;
    }

    /**
     * 根据类别和状态查询菜品信息
     * @param category
     * @param status
     * @return
     */
    @Override
    public List<DishesMessage> selectDishesMessageListByCategoryAndStatus(Long category, String status) {
        return dishesMessageMapper.selectDishesMessageListByCategoryAndStatus(category, status);
    }

    /**
     * 新增菜品信息
     * 
     * @param dishesMessage 菜品信息
     * @return 结果
     */
    @Override
    public int insertDishesMessage(DishesMessage dishesMessage)
    {
        dishesMessage.setDishesId(IdUtils.createId());
        dishesMessage.setCreateBy(SecurityUtils.getUsername());
        dishesMessage.setCreateTime(DateUtils.getNowDate());
        return dishesMessageMapper.insertDishesMessage(dishesMessage);
    }

    /**
     * 修改菜品信息
     * 
     * @param dishesMessage 菜品信息
     * @return 结果
     */
    @Override
    public int updateDishesMessage(DishesMessage dishesMessage)
    {
//        dishesMessage.setUpdateBy(SecurityUtils.getUsername());
        dishesMessage.setUpdateTime(DateUtils.getNowDate());
        return dishesMessageMapper.updateDishesMessage(dishesMessage);
    }

    /**
     * 批量删除菜品信息
     * 
     * @param dishesIds 需要删除的菜品信息主键
     * @return 结果
     */
    @Override
    public int deleteDishesMessageByDishesIds(Long[] dishesIds)
    {
        return dishesMessageMapper.deleteDishesMessageByDishesIds(dishesIds);
    }

    /**
     * 删除菜品信息信息
     * 
     * @param dishesId 菜品信息主键
     * @return 结果
     */
    @Override
    public int deleteDishesMessageByDishesId(Long dishesId)
    {
        return dishesMessageMapper.deleteDishesMessageByDishesId(dishesId);
    }
}
